import requests
import re
import time


url = "https://www.baidu.com"
response = requests.get(url=url)
print(response)
print(dir(response))
print(type(response.content))
content = response.content.decode("UTF8")
print(type(content))
print(content)

title = re.search(r'<title>(.*?)</title>',content)
if title:
    print(title.group(1))

def get_image(url):
    response = requests.get(url=url)
    content = response.content
    print(type(content))
    with open("请求图片/wahaha.jpg", "wb") as f:
        f.write(content)

get_image("https://img-blog.csdnimg.cn/direct/9963237edf104496a3a4cbf9b4002675.png#pic_center")

def school():
    schools = requests.get('https://tieba.baidu.com/t/f/?class=college')
    school1 = schools.content.decode("utf8")
    # print(school1)
    r = re.findall(r'<a class="each_topic_entrance_item" href="//tieba.baidu.com/t/f/.*?" data-fid=".*?">( .*?)</a>',school1)
    # print(r)
    with open("./school.text", 'w', encoding='utf8') as f:
        for i in r:

            f.write(i.replace(" ",""))
            f.write("\n")


school()

# def save_hot():
#     for i in range(13):
#         response = requests.get(f"https://tieba.baidu.com/f/index/rcmdForum?pn={i}&rn=12")
#         # print(response)
#         dates = response.json()['data']['forum_info']
#         # print(dates)
#         for e in dates:
#             # print(e['forum_name'], e['avatar'])
#             name = e['forum_name']
#             avatar_url = e['avatar']
#             print(f"正在处理{name}")
#             avatar_response = requests.get(avatar_url)
#             with open(f"请求图片/{name}.jpg",'wb') as f:
#                 f.write(avatar_response.content)
#
#             time.sleep(1)
#
#
# save_hot()
